package cn.edu.hubu.hubusmartzoohouse.service.impl;

import cn.edu.hubu.hubusmartzoohouse.entity.Animal;
import cn.edu.hubu.hubusmartzoohouse.entity.Device;
import cn.edu.hubu.hubusmartzoohouse.mapper.DeviceMapper;
import cn.edu.hubu.hubusmartzoohouse.service.DeviceService;
import cn.edu.hubu.hubusmartzoohouse.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceImpl implements DeviceService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public ResultVo insert(Device device) {
        int i = deviceMapper.insert(device);
        if (i>0){
            return new ResultVo(200,"添加装置成功",null);
        } else {
            return new ResultVo(500,"操作有误",null);
        }
    }

    @Override
    public ResultVo update(Device device) {
        return null;
    }

    @Override
    public ResultVo delete(Device device) {
        return null;
    }

    @Override
    public ResultVo listDevice() {
        List<Device> list = deviceMapper.listAll();
        ResultVo vo = new ResultVo(200, "查询成功", list);
        return vo;
    }

    @Override
    public ResultVo logicDeleteDevice(int deviceId) {
        int i = deviceMapper.updateIsDelete(deviceId, 1);
        return i>0? new ResultVo(200, "删除成功！", null) :
                new ResultVo(500, "删除失败!", null);
    }

    @Override
    public ResultVo get(int deviceId) {
        Device device = deviceMapper.selectById(deviceId);
        return new ResultVo(200, "查询成功！", device);
    }

    @Override
    public ResultVo modifyDevice(Device device) {
        int i = deviceMapper.updateDevice(device);
        if (i>0){
            return new ResultVo(200, "修改成功！", device);
        } else {
            return new ResultVo(500, "修改失败！", device);

        }
    }

    @Override
    public ResultVo searchDeviceById(int deviceId) {
        Device device = deviceMapper.selectById(deviceId);
        if (device != null){
            return new ResultVo(200, "查询成功！", device);
        } else {
            return new ResultVo(500, "查询失败: 未找到设备信息", null);

        }
    }

    @Override
    public ResultVo searchDeviceByName(String deviceName) {
        Device device = deviceMapper.selectByName(deviceName);
        if (device != null){
            return new ResultVo(200, "查询成功！", device);
        } else {
            return new ResultVo(500, "查询失败: 未找到设备信息", null);

        }
    }
}
